export default function actionCreator(action) {
    return {
        type: action
    }
}


export const REQUEST_POSTS = "REQUEST_POSTS";
export const RECEIVE_POSTS = "RECEIVE_POSTS";

export function requestPosts(subreddit) {
    return {
        type: REQUEST_POSTS,
        subreddit
    }
}

export function receivePosts(subreddit, json) {
    return {
        type: RECEIVE_POSTS,
        subreddit,
        posts: json
    }
}

export function fetchPosts(subreddit) {
    return function(dispatch) {
        dispatch(requestPosts());
        return fetch('http://192.168.112.128:8000/douban_api')
            .then( 
                (response) => response.json(),
                (error) => console.log("An error occurred", error)
            )
            .then( (json) => {
                console.log(json);
                dispatch(receivePosts(subreddit, json))
            })
    }
}